package com.netty.coder;

import com.google.protobuf.GeneratedMessageV3;
import com.message.Message;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * SS通信编码器
 * 
 * @author zhoufang
 *
 *         2017年12月23日下午9:29:30
 */
public class Server2ServerEncoder extends MessageToByteEncoder<Message> {

	@Override
	protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out)
			throws Exception {
		GeneratedMessageV3 body = msg.getBody();
		byte[] byteArray = body.toByteArray();
		int length = 4 + 4 + 4 + byteArray.length;
		out.writeInt(length);
		out.writeInt(msg.getActionId());
		out.writeInt(msg.getResponseId());
		out.writeInt(byteArray.length);
		out.writeBytes(byteArray);
	}

}
